#!/usr/bin/php
<?php
/*
 SPDX-FileCopyrightText: © 2012 Hewlett-Packard Development Company, L.P.

 SPDX-License-Identifier: GPL-2.0-only
*/

/**
 * \file preprocess-php.php
 * \brief replace fields in an input file with values from variables.list.
 * This is the php equivalent of the preprocess script.
 *
 * Usage: preprocess_php variablesfile file2process.in
 *
 * Output is file named file2process (without the suffix)
 *
 * \verbatim
 * variables file looks like:
 *  PROJECT="fossology"
 *  PROJECTUSER="fossy"
 *  etc
 * \endverbatim
 */

/* parse file of variables */
$vars = parse_ini_file($argv[1]);

/* read in input file */
$input_filename = $argv[2];
$filestr = file_get_contents($input_filename);


/* replace each variable */
foreach($vars as $var => $value)
{
  $filestr = str_replace('{$'.$var.'}', $value, $filestr);
}

/* write output file:
 * First get file name less characters after last period.
 * e.g. myfile.php.in  > myfile.php
 */
$PeriodPos = strrpos($input_filename, '.');
$output_filename = substr($input_filename, 0, $PeriodPos);
file_put_contents($output_filename, $filestr);

?>
